package net.yb.view;

import net.yb.entity.Category;
import net.yb.entity.Product;
import net.yb.service.ProductService;
import net.yb.service.impl.ProductServiceImpl;

import java.util.List;
import java.util.Scanner;

/**
 * 启动类
 */
public class Start {

    Scanner input = new Scanner(System.in);
    ProductService productService = new ProductServiceImpl();

    public static void main(String[] args) {
        System.out.println("============================欢迎使用商品管理系统============================");
        new Start().showMenu();
    }

    /**
     * 显示菜单
     */
    public void showMenu(){
        while(true){
            System.out.println("--------------------------------------");
            System.out.println("1. 添加商品");
            System.out.println("2. 修改商品");
            System.out.println("3. 删除商品");
            System.out.println("4. 查询所有商品信息");
            System.out.println("5. 根据id查询商品");
            System.out.println("6. 根据商品名查询商品");
            System.out.println("7. 根据价格区间查询商品");
            System.out.println("8. 查询所有商品分类");
            System.out.println("9. 退出");
            System.out.print("请输入您的选择：");
            int choice = input.nextInt();
            switch (choice){
                case 1:
                    addProduct();
                    break;
                case 2:
                    modifyProduct();
                    break;
                case 3:
                    removeProduct();
                    break;
                case 4:
                    findAllProduct();
                    break;
                case 5:
                    findById();
                    break;
                case 6:
                    findByName();
                    break;
                case 7:
                    findByPrice();
                    break;
                case 8:
                    findAllCategory();
                    break;
                case 9:
                    System.out.println("谢谢使用！");
                    System.exit(0); // 停止JVM
            }
        }
    }

    private void addProduct() {
        System.out.println("------------------------");
        Product product = new Product();
        System.out.print("请输入商品名称：");
        product.setPname(input.next());
        System.out.print("请输入商品价格：");
        product.setPrice(input.nextDouble());
        System.out.print("请输入商品单位：");
        product.setUnit(input.next());
        System.out.print("请输入商品库存：");
        product.setStock(input.nextInt());
        System.out.print("请输入商品分类id：");
        Category category = new Category();
        category.setCid(input.nextInt());
        product.setCategory(category);
        System.out.print("请输入商品详情：");
        product.setInfo(input.next());

        try{
            productService.add(product);
            System.out.println("添加商品成功！");
        }catch (Exception e){
            System.out.println("添加失败：" + e.getMessage());
        }
    }

    private void modifyProduct() {
        System.out.println("------------------------");
        Product product = new Product();
        System.out.print("请输入要修改的商品id：");
        product.setPid(input.nextInt());
        System.out.print("请输入商品名称：");
        product.setPname(input.next());
        System.out.print("请输入商品价格：");
        product.setPrice(input.nextDouble());
        System.out.print("请输入商品单位：");
        product.setUnit(input.next());
        System.out.print("请输入商品库存：");
        product.setStock(input.nextInt());
        System.out.print("请输入商品分类id：");
        Category category = new Category();
        category.setCid(input.nextInt());
        product.setCategory(category);
        System.out.print("请输入商品详情：");
        product.setInfo(input.next());

        try{
            productService.modify(product);
            System.out.println("修改商品成功！");
        }catch (Exception e){
            System.out.println("修改失败：" + e.getMessage());
        }
    }

    private void removeProduct() {
        System.out.println("------------------------");
        System.out.print("请输入要删除的商品id：");
        int id = input.nextInt();
        try{
            productService.removeById(id);
            System.out.println("删除商品成功！");
        }catch (Exception e){
            System.out.println("删除失败：" + e.getMessage());
        }
    }

    private void findAllProduct() {
        List<Product> list = productService.findAll();
        showProductList(list);
    }

    private void findById() {
        System.out.println("------------------------");
        System.out.print("请输入要查询的商品id：");
        int id = input.nextInt();
        Product p = productService.findById(id);
        System.out.println("商品id \t 商品名称\t 商品价格\t 商品单位\t 商品库存\t 商品分类\t 商品详情");
        System.out.println(p.getPid() + "\t\t " + p.getPname() + "\t\t " + p.getPrice() + "\t\t " + p.getUnit() + "\t\t " + p.getStock() + "\t\t " + p.getCategory().getCname() + "\t\t " + p.getInfo());
    }

    private void findByName() {
        System.out.println("------------------------");
        System.out.print("请输入要查询的商品名称：");
        String name = input.next();
        List<Product> list = productService.findByName(name);
        showProductList(list);
    }

    private void findByPrice() {
        System.out.println("------------------------");
        System.out.print("请输入要查询的价格最低值：");
        double minPrice = input.nextDouble();
        System.out.print("请输入要查询的价格最高值：");
        double maxPrice = input.nextDouble();
        List<Product> list = productService.findByPrice(minPrice, maxPrice);
        showProductList(list);
    }

    private void findAllCategory() {

    }

    /**
     * 展示商品列表
     */
    public void showProductList(List<Product> list){
        System.out.println("商品id \t 商品名称\t 商品价格\t 商品单位\t 商品库存\t 商品分类\t 商品详情");
        // 循环输出显示数据
        for(Product p : list){
            System.out.println(p.getPid() + "\t\t " + p.getPname() + "\t\t " + p.getPrice() + "\t\t " + p.getUnit() + "\t\t " + p.getStock() + "\t\t " + p.getCategory().getCname() + "\t\t " + p.getInfo());
        }
    }
}
